﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class RoomBll : IRoomBll
    {
        public RoomBll(IBaseDal<Room> dal)
        {
            Dal = dal;
        }

        public IBaseDal<Room> Dal { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        public async Task<int> AddRoom(Room room)
        {
            return await Dal.AddAsync(room);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelRoom(int id)
        {
            var m= Dal.GetAsync().FirstOrDefault(x => x.Id == id);
            if (m != null)
            {
                return await Dal.DelAsync(m);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public dynamic GetRoom(int index, int size)
        {
            var m= Dal.GetAsync().AsQueryable();
            var c=m.Count();
            var d=m.OrderBy(x=> x.Id).Skip((index-1)*size).Take(size).ToList();
            var res = new
            {
                count = c,
                data = d
            };
            return res;
        }
    }
}
